package com.lagou.task1.work5;

import java.util.*;

public class FightLandlord {

    private String[] cardTypes = {"大王","小王","2","A","K","Q","J","10","9","8","7","6","5","4","3"};

    private List<Card> list = new ArrayList<>();

    private List<Card> ranList = new ArrayList<>();

    private List<Card> dataList1 = new ArrayList<>();

    private List<Card> dataList2 = new ArrayList<>();

    private List<Card> dataList3 = new ArrayList<>();

    private List<String> cardTypeList = Arrays.asList(cardTypes);

    public void initCards(){

        list.add(new Card("大王","大王"));
        list.add(new Card("小王","小王"));

        for(int i=13;i>0;i--){
            String str = "";
            if (i>1 && i<= 10){
                str = i + "";
            }else{
                switch (i){
                    case 1:
                        str = "A";
                        break;
                    case 11:
                        str = "J";
                        break;
                    case 12:
                        str = "Q";
                        break;
                    case 13:
                        str = "K";
                        break;
                }
            }
            Card card1 = new Card("红桃" + str,str);
            Card card2 = new Card("方块" + str,str);
            Card card3 = new Card("黑桃" + str,str);
            Card card4 = new Card("梅花" + str,str);
            list.add(card1);
            list.add(card2);
            list.add(card3);
            list.add(card4);
        }
        System.out.println(list);
    }

    public void ranCard(){
        Random random = new Random();
        int i = 0;
        while (list.size()>0){
            //System.out.println(list.size());
            int ranNum = random.nextInt(list.size());
            Card card = list.get(ranNum);
            ranList.add(card);
            list.remove(ranNum);
        }
        System.out.println(ranList);
    }
    //0 3 6 9 12
    public void sendCard(){
        for (int i = 0; i < ranList.size() - 3; i++) {
            if (i%3 == 0){
                dataList1.add(ranList.get(i));
            }else if (i%3 == 1){
                dataList2.add(ranList.get(i));
            }else{
                dataList3.add(ranList.get(i));
            }
        }
        ranList.removeAll(dataList1);
        ranList.removeAll(dataList2);
        ranList.removeAll(dataList3);
        Collections.sort(dataList1, (o1, o2) -> {
            int num1 = cardTypeList.indexOf(o1.getType());
            int num2 = cardTypeList.indexOf(o2.getType());
            return num1-num2;
        });
        Collections.sort(dataList2, (o1, o2) -> {
            int num1 = cardTypeList.indexOf(o1.getType());
            int num2 = cardTypeList.indexOf(o2.getType());
            return num1-num2;
        });
        Collections.sort(dataList3, (o1, o2) -> {
            int num1 = cardTypeList.indexOf(o1.getType());
            int num2 = cardTypeList.indexOf(o2.getType());
            return num1-num2;
        });
        System.out.println("发牌完成：");
        System.out.println("第一个人的牌是" + dataList1);
        for (Card card : dataList1) {
            System.out.println(card.getName());
        }
        System.out.println("第二个人的牌是" + dataList2);
        for (Card card : dataList2) {
            System.out.println(card.getName());
        }
        System.out.println("第三个人的牌是" + dataList3);
        for (Card card : dataList3) {
            System.out.println(card.getName());
        }
        System.out.println("底牌是" + ranList);
        for (Card card : ranList) {
            System.out.println(card.getName());
        }
    }
}
